<?php

namespace App\Logic;

use App\Component\Qiniu;
use App\Core\BaseCore;
use App\Core\Exception\DefaultException;
use App\Util\CommonHelper;
use App\Util\UserIdentityHelper;
use EasySwoole\Component\Singleton;
use EasySwoole\EasySwoole\Config;

class UploadService extends BaseCore {

    use Singleton;

    /**
     * 上次文件
     * @param $file
     */
    public function upload($file){
        if ( empty($file) ){
            throw new DefaultException('请上传文件！');
        }

        $size = $file->getSize();
        if ( empty($size) ){
            throw new DefaultException('请上传文件！');
        }

//        //获取文件类型
//        $extension = CommonHelper::getExtension($file->getClientFilename());
//        //新文件名
//        $file_name =  date('Ymd', time()) .'/'. \EasySwoole\Utility\Random::makeUUIDV4().".$extension";
//
//        //上传云
//        $qiniu = new Qiniu();
//        $info = $qiniu->upload($file->getTempName(),$file_name);

        $file_path = $this->getFile($file);
        $file->moveTo($file_path);
        $file_path = str_replace(EASYSWOOLE_ROOT . "/", '', $file_path);

        /**
         * 保存上传文件信息到UploadModel
        */
        $uploadModel = new \App\Model\Common\UploadModel();
        $data = [
            'user_id' => UserIdentityHelper::getId(),
            'file_path' => $file_path,
            'org_name' => $file->getClientFilename(),
        ];
        $uploadModel::create()->insertData($data);
        $file_path = Config::getInstance()->getConf('HOST') . $file_path;
        return $this->success(['path' => $file_path]);

    }


    private function getFile($file) {
        //获取文件类型
        $extension = CommonHelper::getExtension($file->getClientFilename());
        if(empty($extension)){
            $extension = 'mp4';
        }

        $dir = EASYSWOOLE_ROOT  . "/upload/". date("Ym");
        if(!is_dir($dir)) {
            mkdir($dir, 0777 , true);
        }

        $basename = "/" . \EasySwoole\Utility\Random::makeUUIDV4() . ".".$extension;
        return  $dir . $basename;
    }


    /**
     * 上次文件 二进制
     * @param $file
     */
    public function uploadBinary($request){
        /**上传文件格式*/
        $allowedFiles = [
            'video/mp4' => 'mp4'
        ];
        $fileType = $request->getHeader('content-type')[0];
        if(!isset($allowedFiles[$fileType])){
            throw new DefaultException('上传文件格式错误！');
        }
        $dir = EASYSWOOLE_ROOT  . "/upload/". date("Ym");
        if(!is_dir($dir)) {
            mkdir($dir, 0777 , true);
        }
        $extension = $allowedFiles[$fileType];
        $basename = "/" . \EasySwoole\Utility\Random::makeUUIDV4() . ".".$extension;
        $file_path =  $dir . $basename;
        $content = $request->getSwooleRequest()->rawContent();
        $content = base64_decode(str_replace("data:application/octet-stream;base64,","", $content));
        file_put_contents($file_path, $content);
        $file_path = str_replace(EASYSWOOLE_ROOT . "/", '', $file_path);
        return $this->success(['path' => $file_path]);
    }


}